/*************************************************
* Title：TinyToolKit
* Author：Opportunity 
* Discribe：跟时间相关的工具方法
* CreateTime：2020-04-05 20:21:58
* Version：1.0
* Modify Recoder：
*************************************************/
using System;

namespace TinyToolKit
{
    public static partial class TinyUtility
    {
        public static class Time   
        {
            /// <summary>
            /// 获取时间戳，从1970年1月1日零点到现在的秒数
            /// </summary>
            /// <returns></returns>
            public static long GetTimeStamp()
            {
                TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
                return Convert.ToInt64(ts.TotalSeconds);
            }
            
            /// <summary>
            /// 转化秒数为时分秒的格式
            /// </summary>
            /// <returns>返回时间</returns>
            /// <param name="secondTime">需要转化的秒数</param>
            public static string ConvertSecondsToTime(int secondTime, bool showHour = true)
            {
                int hour = secondTime / (60 * 60);
                int minute = (secondTime % (60 * 60)) / 60;
                int second = secondTime % 60;

                // return string.Format("{0}:{1}:{2}", hours, minutes.ToString().PadLeft(2, '0'), seconds.ToString().PadLeft(2, '0'));
                if (showHour)
                    return string.Format("{0:D2}:{1:D2}:{2:D2}", hour, minute, second);
                else
                    return string.Format("{0:D2}:{1:D2}", minute, second);
            }

            /// <summary>
            /// 返回本地时间
            /// </summary>
            public static string GetNowLocalTime()
            {
                return GetLocalDateTime().ToString();
            }

            /// <summary>
            /// 获取年份
            /// </summary>
            public static int GetYear()
            {
                return GetLocalDateTime().Year;
            }

            /// <summary>
            /// 获取月份
            /// </summary>
            public static int GetMonth()
            {
                return GetLocalDateTime().Month;
            }

            /// <summary>
            /// 获取日期
            /// </summary>
            public static int GetDay()
            {
                return GetLocalDateTime().Day;
            }

            /// <summary>
            /// 获取星期
            /// </summary>
            public static int GetWeek()
            {
                return (int)GetLocalDateTime().DayOfWeek;
            }

            /// <summary>
            /// 获取时钟
            /// </summary>
            public static int GetHour()
            {
                return GetLocalDateTime().Hour;
            }

            /// <summary>
            /// 获取分钟
            /// </summary>
            public static int GetMinute()
            {
                return GetLocalDateTime().Minute;
            }

            /// <summary>
            /// 获取秒钟
            /// </summary>
            public static int GetSecond()
            {
                return GetLocalDateTime().Second;
            }

            private static DateTime GetLocalDateTime()
            {
                DateTime startDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0);
                double nowTime = (DateTime.UtcNow - startDateTime).TotalMilliseconds;
                return TimeZone.CurrentTimeZone.ToLocalTime(startDateTime.AddMilliseconds(nowTime));
            }
        }
    }
}